package com.yyn.vod.service.Impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.yyn.servicebase.exceptionhandler.CustomException;
import com.yyn.vod.config.GetUser;
import com.yyn.vod.service.VodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.yyn.vod.utils.InitObject.initVodClient;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadGetId(MultipartFile file)  {
        try {
            //fileName：上传文件原始名称
            String fileName = file.getOriginalFilename();
            //title：上传之后显示名称(例子：01.mp4=>01)
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream：上传文件输入流
            InputStream inputStream = file.getInputStream();
            //获得request
            UploadStreamRequest request = new UploadStreamRequest(GetUser.ACCESS_KEY_ID, GetUser.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            //上传获得response
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                //得到视频id
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }




    }

    @Override
    public boolean removeVideo(String videoId) throws ClientException {
//        初始化
        DefaultAcsClient client = initVodClient(GetUser.ACCESS_KEY_ID, GetUser.ACCESS_KEY_SECRET);
//        request这一步已经执行删除了
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        DeleteVideoResponse  response = new DeleteVideoResponse ();
        try {
//            删除后返回的值   没啥用
            DeleteVideoResponse acsResponse = client.getAcsResponse(request);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(20001,"删除视频失败");
        }
    }

    @Override
    public String getVideoAuth(String id) throws ClientException {
//        1、初始化
        GetUser getUser = new GetUser();
//        2、response、request
        DefaultAcsClient client = initVodClient(GetUser.ACCESS_KEY_ID, GetUser.ACCESS_KEY_SECRET);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(id);
        String playAuth = null;
        try {
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            //播放凭证
            playAuth = response.getPlayAuth();

        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        return playAuth;
    }


}
